package com.uenpay.oem.ui.register;

import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.TextInputLayout;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.uenpay.im.IMApplication;
import com.uenpay.oem.R;
import com.uenpay.oem.common.AppConstant;
import com.uenpay.oem.common.RxBusEventConstant;
import com.uenpay.oem.ui.settings.SettingsActivity;
import com.uenpay.oem.util.camera.PhoneUtils;
import com.uenpay.util.app.base.BaseActivity;
import com.uenpay.util.common.EditFormatUtils;
import com.uenpay.util.rx.CommonEvent;
import com.uenpay.util.rx.RxBus;

import butterknife.BindView;
import butterknife.OnClick;
import rx.Observable;
import rx.functions.Action1;

/**
 * Created by PS on 2017/6/22.
 */

public class ReglsterActivitycopy extends BaseActivity implements RegisterContract.View {

	public static final String TAG = "ReglsterActivitycopy";
	@BindView(R.id.account_edit)
	EditText accountEdit;
	@BindView(R.id.register_input_layout)
	TextInputLayout register_input_ly;
	@BindView(R.id.register_text)
	TextView registertext;

	private boolean getFocusFlag = false;
	private RegisterContract.Presenter MRegisterpresenter;
	private String account_name;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

	}

	@Override
	public View bindView() {
		return null;
	}

	@Override
	public int bindLayout() {
		return R.layout.activity_register_layoutcopy;
	}

	@Override
	public void initView(View view) {
		setPresenter(null);
		// is_register = getIntent().getBooleanExtra("is_register", true);

		setTitle("手机号注册");

		getFocusFlag = true;
		EditFormatUtils.phoneNumAddSpace(accountEdit);
		register_input_ly.setOnFocusChangeListener(new View.OnFocusChangeListener() {
			@Override
			public void onFocusChange(View v, boolean hasFocus) {
				if (hasFocus) {
					register_input_ly.setHint("请输入未注册过的手机号码");
					getFocusFlag = true;
				} else {
					register_input_ly.setHint("                 请输入未注册过的手机号码");
				}
			}
		});

		Observable<CommonEvent> observable = RxBus.$().register(TAG);
		RxBus.$().OnEvent(observable, new Action1<Object>() {

			@Override
			public void call(Object obj) {
				CommonEvent event = (CommonEvent) obj;
				if (event.getWhat() == RxBusEventConstant.EVENT_REGISTER_CODE) {

					MRegisterpresenter.Sendauthcode(account_name);
				}
			}
		});
	}

	@Override
	protected void onResume() {
		super.onResume();
		if (getFocusFlag) {
			register_input_ly.setHint("请输入未注册过的手机号码");
			accountEdit.setFocusableInTouchMode(true);
			accountEdit.setFocusable(true);
			accountEdit.requestFocus();

			registertext.setFocusable(true);
			registertext.setFocusableInTouchMode(true);
			registertext.requestFocus(); // 初始不让EditText得焦点
			registertext.requestFocusFromTouch();
		}
	}

	// 点击注册

	@Override
	public void showLoading() {

	}

	@Override
	public void dismissLoading() {

	}

	@Override
	public void setPresenter(Object presenter) {
		MRegisterpresenter = new RegisterPresenter(this);
	}

	@Override
	public void showToast(String msg) {

	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		RxBus.$().unregister(this);
	}

	@Override
	public void Phonelegal() {
		MRegisterpresenter.Sendauthcode(account_name);
		// 走到这里就说明 手机号可以用 发送验证码
		Intent intent = new Intent(ReglsterActivitycopy.this, SettingsPswActivity.class);
		intent.putExtra("account_name", account_name);
		startActivityForResult(intent, AppConstant.REGISTER_REQUEST_CODE);
	}

	@OnClick({ R.id.iv_closenumber, R.id.get_verification_code_btn })
	public void onClick(View view) {
		switch (view.getId()) {
		case R.id.iv_closenumber:
			accountEdit.setText("");

			break;
		case R.id.get_verification_code_btn:
			register_input_ly.setErrorEnabled(false);
			account_name = accountEdit.getText().toString().trim().replace(" ", "");
			if (!TextUtils.isEmpty(account_name)) {
				if (account_name.startsWith("1") && account_name.length() == 11) {
					if (PhoneUtils.isMobile(account_name)) {
						MRegisterpresenter.isPhoneRegister(account_name);
					} else {
						IMApplication.showToast(getResources().getString(com.uenpay.im.R.string.phone_number_error));
					}

				} else {
					IMApplication.showToast(getResources().getString(com.uenpay.im.R.string.phone_number_error));
				}

			} else {
				IMApplication.showToast(getResources().getString(com.uenpay.im.R.string.phone_number_error));
				return;
			}

			break;
		}
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		if (requestCode == AppConstant.REGISTER_REQUEST_CODE && resultCode == RESULT_OK) {
			finish();
		}
	}
}
